bothraces1 <- bothraces %>% filter(days <=150)
eitherrace1 <- eitherrace %>% filter(days <=150)
neitherrace1 <- neitherrace %>% filter(days <=150)

violent1_race <- lm(violence_range ~ days * treatment, bothraces1, weights = weight)
summary(violent1_race)

violent2_race <- lm(violence_range ~ days * treatment, eitherrace1, weights = weight)                             
summary(violent2_race)
c
violent3_race <- lm(violence_range ~ days * treatment, neitherrace1, weights = weight)
summary(violent3_race)

violent1_data <- 
  data.frame(get_model_data(violent1_race, type = 'pred', 
                            terms = c('treatment', 'days[-50, -40, -30, -20, -10, 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150]'))) %>% 
  mutate(group = as.numeric(as.character(group)))

violent2_data <- 
  data.frame(get_model_data(violent2_race, type = 'pred', 
                            terms = c('treatment', 'days[-50, -40, -30, -20, -10, 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150]'))) %>% 
  mutate(group = as.numeric(as.character(group)))

violent3_data <- 
  data.frame(get_model_data(violent3_race, type = 'pred', 
                            terms = c('treatment', 'days[-50, -40, -30, -20, -10, 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150]'))) %>% 
  mutate(group = as.numeric(as.character(group)))

bb1 <- violent1_data %>% filter(x == 0, group < 1) %>% mutate(which = '1')
bb2 <- violent1_data %>% filter(x == 1, group > -1) %>% mutate(which = '1')
bb3 <- violent2_data %>% filter(x == 0, group < 1) %>% mutate(which = '2')
bb4 <- violent2_data %>% filter(x == 1, group > -1) %>% mutate(which = '2')
bb5 <- violent3_data %>% filter(x == 0, group < 1) %>% mutate(which = '3')
bb6 <- violent3_data %>% filter(x == 1, group > -1) %>% mutate(which = '3')

b1 <- 
  ggplot()+
  geom_line(data = bb1, aes(x=predicted, y = group, color = which, linetype = which), size = 1.1)+
  geom_ribbon(data = bb1, 
              aes(x=predicted, y = group, xmin = conf.low, xmax = conf.high, fill = which), alpha = 0.3)+
  geom_line(data = bb2, aes(x=predicted, y = group, color = which, linetype = which), size = 1.1)+
  geom_ribbon(data = bb2, 
              aes(x=predicted, y = group, xmin = conf.low, xmax = conf.high, fill = which), alpha = 0.3)+
  geom_line(data = bb3, aes(x=predicted, y = group, color = which, linetype = which), size = 1.1)+
  geom_ribbon(data = bb3, 
              aes(x=predicted, y = group, xmin = conf.low, xmax = conf.high, fill = which), alpha = 0.3)+
  geom_line(data = bb4, aes(x=predicted, y = group, color = which, linetype = which), size = 1.1)+
  geom_ribbon(data = bb4, 
              aes(x=predicted, y = group, xmin = conf.low, xmax = conf.high, fill = which), alpha = 0.3)+
  geom_line(data = bb5, aes(x=predicted, y = group, color = which, linetype = which), size = 1.1)+
  geom_ribbon(data = bb5, 
              aes(x=predicted, y = group, xmin = conf.low, xmax = conf.high, fill = which), alpha = 0.3)+
  geom_line(data = bb6, aes(x=predicted, y = group, color = which, linetype = which), size = 1.1)+
  geom_ribbon(data = bb6, 
              aes(x=predicted, y = group, xmin = conf.low, xmax = conf.high, fill = which), alpha = 0.3)+
  geom_hline(yintercept = 0, color = 'red', linetype=2)+
  xlim(-.13,.5)+
  scale_y_continuous(breaks = seq(-50,150,10))+
  ylab('Days before/after 2022 Election')+
  xlab('Support for Political Violence')+
  coord_flip()+
  scale_color_manual(values=c("#cc6677", "#E69F00", "#56B4E9"),
                     labels=c('Both Races', 'One Race', 'No Race'))+
  scale_fill_manual(values=c("#cc6677", "#E69F00", "#56B4E9"),
                    labels=c('Both Races', 'One Race', 'No Race'))+
  scale_linetype(labels=c('Both Races', 'One Race', 'No Race'))+
  labs(fill = "Number of Races", linetype = 'Number of Races', color = 'Number of Races')+
  theme_bw()


norms1_race <- lm(norms_range ~ days * treatment, bothraces1, weights = weight)
summary(norms1_race)

norms2_race <- lm(norms_range ~ days * treatment, eitherrace1, weights = weight)
summary(norms2_race)

norms3_race <- lm(norms_range ~ days * treatment, neitherrace1, weights = weight)
summary(norms3_race)

norms1_data <- 
  data.frame(get_model_data(norms1_race, type = 'pred', 
                            terms = c('treatment', 'days[-50, -40, -30, -20, -10, 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150]'))) %>% 
  mutate(group = as.numeric(as.character(group)))

norms2_data <- 
  data.frame(get_model_data(norms2_race, type = 'pred', 
                            terms = c('treatment', 'days[-50, -40, -30, -20, -10, 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150]'))) %>% 
  mutate(group = as.numeric(as.character(group)))

norms3_data <- 
  data.frame(get_model_data(norms3_race, type = 'pred', 
                            terms = c('treatment', 'days[-50, -40, -30, -20, -10, 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150]'))) %>% 
  mutate(group = as.numeric(as.character(group)))

nnorms1 <- norms1_data %>% filter(x == 0, group < 1) %>% mutate(which = '1')
nnorms2 <- norms1_data %>% filter(x == 1, group > -1) %>% mutate(which = '1')
nnorms3 <- norms2_data %>% filter(x == 0, group < 1) %>% mutate(which = '2')
nnorms4 <- norms2_data %>% filter(x == 1, group > -1) %>% mutate(which = '2')
nnorms5 <- norms3_data %>% filter(x == 0, group < 1) %>% mutate(which = '3')
nnorms6 <- norms3_data %>% filter(x == 1, group > -1) %>% mutate(which = '3')

b2 <- 
  ggplot()+
  geom_line(data = nnorms1, aes(x=predicted, y = group, color = which, linetype = which), size = 1.1)+
  geom_ribbon(data = nnorms1, 
              aes(x=predicted, y = group, xmin = conf.low, xmax = conf.high, fill = which), alpha = 0.3)+
  geom_line(data = nnorms2, aes(x=predicted, y = group, color = which, linetype = which), size = 1.1)+
  geom_ribbon(data = nnorms2, 
              aes(x=predicted, y = group, xmin = conf.low, xmax = conf.high, fill = which), alpha = 0.3)+
  geom_line(data = nnorms3, aes(x=predicted, y = group, color = which, linetype = which), size = 1.1)+
  geom_ribbon(data = nnorms3, 
              aes(x=predicted, y = group, xmin = conf.low, xmax = conf.high, fill = which), alpha = 0.3)+
  geom_line(data = nnorms4, aes(x=predicted, y = group, color = which, linetype = which), size = 1.1)+
  geom_ribbon(data = nnorms4, 
              aes(x=predicted, y = group, xmin = conf.low, xmax = conf.high, fill = which), alpha = 0.3)+
  geom_line(data = nnorms5, aes(x=predicted, y = group, color = which, linetype = which), size = 1.1)+
  geom_ribbon(data = nnorms5, 
              aes(x=predicted, y = group, xmin = conf.low, xmax = conf.high, fill = which), alpha = 0.3)+
  geom_line(data = nnorms6, aes(x=predicted, y = group, color = which, linetype = which), size = 1.1)+
  geom_ribbon(data = nnorms6, 
              aes(x=predicted, y = group, xmin = conf.low, xmax = conf.high, fill = which), alpha = 0.3)+
  geom_hline(yintercept = 0, color = 'red', linetype=2)+
  xlim(0,.7)+
  scale_y_continuous(breaks = seq(-50,150,10))+
  ylab('Days before/after 2022 Election')+
  xlab('Support for Norm Violations')+
  coord_flip()+
  scale_color_manual(values=c("#cc6677", "#E69F00", "#56B4E9"),
                     labels=c('Both Races', 'One Race', 'No Race'))+
  scale_fill_manual(values=c("#cc6677", "#E69F00", "#56B4E9"),
                    labels=c('Both Races', 'One Race', 'No Race'))+
  scale_linetype(labels=c('Both Races', 'One Race', 'No Race'))+
  labs(fill = "Number of Races", linetype = 'Number of Races', color = 'Number of Races')+
  theme_bw()



affpol1_race <- lm(affpol ~ days * treatment, bothraces1, weights = weight)
summary(affpol1_race)

affpol2_race <- lm(affpol ~ days * treatment, eitherrace1, weights = weight)
summary(affpol2_race)

affpol3_race <- lm(affpol ~ days * treatment, neitherrace1, weights = weight)
summary(affpol3_race)

affpol1_data <- 
  data.frame(get_model_data(affpol1_race, type = 'pred', 
                            terms = c('treatment', 'days[-50, -40, -30, -20, -10, 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150]'))) %>% 
  mutate(group = as.numeric(as.character(group)))

affpol2_data <- 
  data.frame(get_model_data(affpol2_race, type = 'pred', 
                            terms = c('treatment', 'days[-50, -40, -30, -20, -10, 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150]'))) %>% 
  mutate(group = as.numeric(as.character(group)))

affpol3_data <- 
  data.frame(get_model_data(affpol3_race, type = 'pred', 
                            terms = c('treatment', 'days[-50, -40, -30, -20, -10, 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150]'))) %>% 
  mutate(group = as.numeric(as.character(group)))

aaffpol1 <- affpol1_data %>% filter(x == 0, group < 1) %>% mutate(which = '1')
aaffpol2 <- affpol1_data %>% filter(x == 1, group > -1) %>% mutate(which = '1')
aaffpol3 <- affpol2_data %>% filter(x == 0, group < 1) %>% mutate(which = '2')
aaffpol4 <- affpol2_data %>% filter(x == 1, group > -1) %>% mutate(which = '2')
aaffpol5 <- affpol3_data %>% filter(x == 0, group < 1) %>% mutate(which = '3')
aaffpol6 <- affpol3_data %>% filter(x == 1, group > -1) %>% mutate(which = '3')

b3 <- 
  ggplot()+
  geom_line(data = aaffpol1, aes(x=predicted, y = group, color = which, linetype = which), size = 1.1)+
  geom_ribbon(data = aaffpol1, 
              aes(x=predicted, y = group, xmin = conf.low, xmax = conf.high, fill = which), alpha = 0.3)+
  geom_line(data = aaffpol2, aes(x=predicted, y = group, color = which, linetype = which), size = 1.1)+
  geom_ribbon(data = aaffpol2, 
              aes(x=predicted, y = group, xmin = conf.low, xmax = conf.high, fill = which), alpha = 0.3)+
  geom_line(data = aaffpol3, aes(x=predicted, y = group, color = which, linetype = which), size = 1.1)+
  geom_ribbon(data = aaffpol3, 
              aes(x=predicted, y = group, xmin = conf.low, xmax = conf.high, fill = which), alpha = 0.3)+
  geom_line(data = aaffpol4, aes(x=predicted, y = group, color = which, linetype = which), size = 1.1)+
  geom_ribbon(data = aaffpol4, 
              aes(x=predicted, y = group, xmin = conf.low, xmax = conf.high, fill = which), alpha = 0.3)+
  geom_line(data = aaffpol5, aes(x=predicted, y = group, color = which, linetype = which), size = 1.1)+
  geom_ribbon(data = aaffpol5, 
              aes(x=predicted, y = group, xmin = conf.low, xmax = conf.high, fill = which), alpha = 0.3)+
  geom_line(data = aaffpol6, aes(x=predicted, y = group, color = which, linetype = which), size = 1.1)+
  geom_ribbon(data = aaffpol6, 
              aes(x=predicted, y = group, xmin = conf.low, xmax = conf.high, fill = which), alpha = 0.3)+
  geom_hline(yintercept = 0, color = 'red', linetype=2)+
  xlim(0,100)+
  scale_y_continuous(breaks = seq(-50,150,10))+
  ylab('Days before/after 2022 Election')+
  xlab('Support for Norm Violations')+
  coord_flip()+
  scale_color_manual(values=c("#cc6677", "#E69F00", "#56B4E9"),
                     labels=c('Both Races', 'One Race', 'No Race'))+
  scale_fill_manual(values=c("#cc6677", "#E69F00", "#56B4E9"),
                    labels=c('Both Races', 'One Race', 'No Race'))+
  scale_linetype(labels=c('Both Races', 'One Race', 'No Race'))+
  labs(fill = "Number of Races", linetype = 'Number of Races', color = 'Number of Races')+
  theme_bw()

ff2 <- suppressMessages(ggpubr::ggarrange(b1,b2,b3, ncol = 1))
suppressMessages(print(ff2))
#ggsave(ff2, file = 'ff2.png', units = 'in', height = 8, width = 10)